package edu.hdu.hangzhe.bytecode;

import edu.hdu.hangzhe.bytecode.domain.ByteCode;
import edu.hdu.hangzhe.bytecode.domain.Field;
import edu.hdu.hangzhe.bytecode.domain.Method;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

public class MyClassVisitor extends ClassVisitor {

    private ByteCode byteCode;

    public MyClassVisitor(int api) {
        super(api);
    }
    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        byteCode = new ByteCode(name);
        super.visit(version, access, name, signature, superName, interfaces);
    }

    @Override
    public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
        System.out.println("field:" + name + " " + descriptor + " " + signature + " " + (value != null ? value.toString() : "null"));
        byteCode.addField(name, new Field(name, descriptor));
        return super.visitField(access, name, descriptor, signature, value);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        System.out.println("=========================method:" + name + "(" + descriptor + ")=========================");
        Method m = new Method(name + "(" + descriptor + ")", access, descriptor);
        byteCode.addMethod(name + "(" + descriptor + ")", m);
        return new MyMethodVisitor(this.api, m);
    }

    public ByteCode getByteCode() {
        return byteCode;
    }
}

